package model

import (
	"fmt"
	"time"
	"yunj/pkg/global"
	"yunj/pkg/yunj/app/enum/stateenum"
	"yunj/pkg/yunj/core/builder/builderdto"
	"yunj/pkg/yunj/util"
)

type AdminRole struct {
	Id        uint64        `gorm:"primary_key;autoIncrement;column:id" json:"id,omitempty" form:"id"`
	Name      string        `gorm:"column:name" json:"name" form:"name"`
	Code      string        `gorm:"column:code;uniqueIndex" json:"code" form:"code"`
	Intro     string        `gorm:"column:intro" json:"intro" form:"intro"`
	Auths     StrSliceJson  `gorm:"column:auths;type:json" json:"auths" form:"auths"`
	Sort      int           `gorm:"column:sort" json:"sort" form:"sort"`
	Members   []AdminMember `gorm:"many2many:admin_member_role;joinForeignKey:role_id;joinReferences:member_id"`
	CreatedAt *time.Time    `gorm:"column:created_at" json:"created_at,omitempty" form:"created_at"`
	UpdatedAt *time.Time    `gorm:"column:updated_at" json:"updated_at,omitempty" form:"updated_at"`
	State     uint8         `gorm:"column:state" json:"state" form:"state"`
}

// 获取正常角色枚举选项
func GetNormalRoleTableColOptions() (options []builderdto.TableColOption, err error) {
	var roles []*AdminRole
	err = global.MySQL.Model(&AdminRole{}).Select("id", "name").Where("state = ?", stateenum.NORMAL).Order("sort asc").Find(&roles).Error
	if err != nil {
		err = fmt.Errorf("获取正常角色枚举选项数据查询异常！%v", err)
		return
	}
	options = make([]builderdto.TableColOption, 0, len(roles))
	for _, role := range roles {
		label := role.Name
		options = append(options, builderdto.TableColOption{
			Value:   role.Id,
			Label:   label,
			BgColor: util.RandomHexColor("#ffffff"),
		})
	}
	return
}
